package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.controller.GuideController;
import com.example.springboot.entity.Guide;
import com.example.springboot.entity.Note;
import com.example.springboot.entity.Patent;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.NoteMapper;
import com.example.springboot.service.IGuideService;
import com.example.springboot.service.INoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.IPatentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {

    @Resource
    private INoteService noteService;

    @Resource
    private IGuideService guideService;

    @Resource
    private IPatentService patentService;


    @Override   //插入笔记
    public Result insert(Note note) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patent_id",note.getPatentId());
        queryWrapper.eq("guide_id",note.getGuideId());
        Note one;
        Guide guide;
        try {
            guide = guideService.getById(note.getGuideId());
            one = noteService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (guide == null || !note.getUserId().equals(guide.getUserId()))
            return Result.error(Constants.CODE_600,"引导不存在");
        if (one != null){
            return Result.error(Constants.CODE_600,"该专利已被摘录！");
        }
        else {
            return Result.success("保存成功!",noteService.save(note));
        }
    }

    @Override
    public Result findByGuide(Integer guideId) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("guide_id",guideId);
        return Result.success(noteService.getOne(queryWrapper));
    }

    @Override
    public Result searchByGuide(Integer userId, String guideName) {
        List<Guide> guideList = guideService.findByName(userId,guideName);   //根据用户id和搜索引导名称查找引导id列表
        List<Note> noteList = new ArrayList<>();
        for (Guide guide : guideList) {         //对每个引导id在笔记表中查找笔记
            QueryWrapper<Note> wrapper = new QueryWrapper<>();
            wrapper.eq("guide_id",guide.getId());
            List<Note> tempList = noteService.list(wrapper);    //查找到的笔记列表
            for (Note note : tempList) {        //对笔记列表中的每个笔记 设置引导名称，专利名称和专利链接
                note.setGuideName(guide.getName());
                Patent patent = patentService.getById(note.getPatentId());
                if (patent != null){
                    note.setPatentName(patent.getName());
                    note.setPatentUrl(patent.getUrl());
                }
            }
            noteList.addAll(tempList);
        }
        return Result.success(noteList);
    }

    @Override
    public Result getByUidAndGid(Integer userId, Integer guideId) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("guide_id",guideId);
        return Result.success(noteService.list(queryWrapper));
    }

    @Override
    public boolean deleteByGuide(Integer guideId) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("guide_id",guideId);
        return noteService.remove(queryWrapper);
    }

    @Override   //保存笔记
    public Result saveNote(Note note) {
        Note one;
        Guide guide;
        try {
            one = noteService.getById(note.getId());
            guide = guideService.getById(note.getGuideId());
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (guide == null || !note.getUserId().equals(guide.getUserId()))
            return Result.error(Constants.CODE_600,"引导不存在");
        if (one == null || !note.getUserId().equals(one.getUserId()))
            return Result.error(Constants.CODE_600,"笔记不存在");
        if (saveOrUpdate(note))
            return Result.success("保存成功");
        return Result.error(Constants.CODE_600,"保存失败");
    }
}
